<!--
 * @Author: weikang
 * @Description:
 * @Date: 2024-01-07 15:06:18
 * @LastEditTime: 2024-02-29 20:16:30
 * @FilePath: \env-manager-pc\src\views\document\device\detail\model\duibi.vue
-->
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { useDuiBiForm, useEnumInfos } from './page.helper'
import documentApi from '@/api/modules/document'

const { deviceId } = defineProps({
  deviceId: {
    type: String,
    default: '',
  },
})

const options = useEnumInfos()
const loading = ref(false)

const { formData } = useDuiBiForm()

function getDuiBiData() {
  documentApi.DeviceGetDeviceElectricArchive({
    deviceObjIDs: [deviceId],
  }).then((res) => {
    formData.value.TAIn = res.TAIn
    formData.value.TAOut = res.TAOut
    formData.value.TVIn = res.TVIn
    formData.value.TVOut = res.TVOut
    formData.value.WingWay = res.WingWay
  })
}
getDuiBiData()

function callDuibiData() {
  loading.value = true
  documentApi.DeviceCallMeterParam({
    deviceObjID: deviceId,
  }).then((res) => {
    formData.value.TAIn = res.TAIn
    formData.value.TAOut = res.TAOut
    formData.value.TVIn = res.TVIn
    formData.value.TVOut = res.TVOut
    formData.value.WingWay = res.WingWay
    loading.value = false
  }, () => {
    loading.value = false
  })
}

function setDuiBIData() {
  loading.value = true
  documentApi.DeviceSetMeterParam({
    deviceObjID: deviceId,
    meterParam: formData.value,
  }).then(() => {
    ElMessage.success('下发成功')
    loading.value = false
  }, () => {
    loading.value = false
  })
}
</script>

<template>
  <div>
    <el-card class="box-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>CT/PT</span>
          <div>
            <el-button type="primary" link :loading="loading" @click="callDuibiData">
              召测
            </el-button>
            <el-button type="warning" link :loading="loading" @click="setDuiBIData">
              下发
            </el-button>
          </div>
        </div>
      </template>
      <el-form v-loading="loading" label-width="120px" label-suffix="：">
        <el-row>
          <el-col :span="12">
            <el-form-item label="电流变比">
              <el-col :span="10">
                <el-input-number v-model="formData.TAIn" class="w-full!" :controls="false" />
              </el-col>
              <e-col :span="2">
                /
              </e-col>
              <el-col :span="11">
                <el-input-number v-model="formData.TAOut" class="w-full!" :controls="false" />
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电压变比">
              <el-col :span="11">
                <el-input-number v-model="formData.TVIn" class="w-full!" :controls="false" />
              </el-col>
              <e-col :span="2">
                /
              </e-col>
              <el-col :span="11">
                <el-input-number v-model="formData.TVOut" class="w-full!" :controls="false" />
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接线方式">
              <el-select v-model="formData.WingWay" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.ID"
                  :label="item.Name"
                  :value="Number(item.ID)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.card-header {
  --at-apply: flex flex-justify-between;
}
</style>
